package com.tomecode.tools.maven.settings.gui.tabs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import javax.swing.AbstractListModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;

import com.tomecode.tools.maven.settings.gui.Desktop;
import com.tomecode.tools.maven.settings.gui.FrmLoading;
import com.tomecode.tools.maven.settings.gui.compents.UpdatePolicyPanel;
import com.tomecode.tools.maven.settings.model.Activation;
import com.tomecode.tools.maven.settings.model.ChecksumPolicy;
import com.tomecode.tools.maven.settings.model.File;
import com.tomecode.tools.maven.settings.model.Layout;
import com.tomecode.tools.maven.settings.model.Os;
import com.tomecode.tools.maven.settings.model.PluginRepository;
import com.tomecode.tools.maven.settings.model.Profile;
import com.tomecode.tools.maven.settings.model.Property;
import com.tomecode.tools.maven.settings.model.Release;
import com.tomecode.tools.maven.settings.model.Repository;
import com.tomecode.tools.maven.settings.model.Settings;
import com.tomecode.tools.maven.settings.model.Snapshot;
import com.tomecode.tools.maven.settings.model.UpdatePolicy;
import com.tomecode.tools.maven.settings.util.URLTester;
import com.tomecode.util.gui.HideNotifiListener;
import com.tomecode.util.gui.PanelFactory;
import com.tomecode.util.gui.component.Button;

/**
 * Edit {@link Profile}
 * 
 * @author Frastia Tomas
 * 
 */
public final class ProfilesTab extends JPanel {

	private static final long serialVersionUID = -2875024946445977733L;

	private final Button bSave;
	private final Button bEdit;
	private final Button bDelete;

	private final JList listProfiles;
	private final ListProfilesModel listProfilesModel;

	private final JTextField txtProfileId;

	private final JCheckBox checkActiveByDefault;
	private final JTextField txtActivationJdk;
	private final JTextField txtOsName;
	private final JTextField txtOsFamily;
	private final JTextField txtOsArch;
	private final JTextField txtOsVersion;

	private final JTextField txtPropertyName;
	private final JTextField txtPropertyValue;

	private final Button bPropertiesSave;
	private final Button bPropertiesEdit;
	private final Button bPropertiesDelete;

	private final JTextField txtFileExists;
	private final JTextField txtFileMissing;

	private final PropertiesTableModel tablePropertiesModel;
	private final JTable tableProperties;

	private final JTextField txtPropertiesName;
	private final JTextField txtPropertiesValue;

	private final RepositoriesTableModel tableRepositoriesModel;
	private final JTable tableRepositories;

	private final Button bRepositoriesSave;
	private final Button bRepositoriesEdit;
	private final Button bRepositoriesDelete;

	private final JTextField txtRepositoriesId;
	private final JTextField txtRepositoriesName;
	private final JTextField txtRepositoriesUrl;
	private final JComboBox comboRepositoriesLayout;

	private final JCheckBox checRepositorySnapshotEnabled;
	private final UpdatePolicyPanel panelRepositorySnapshotUpdatePolicy;
	private final JComboBox comboRepositorySnapshotChecksumPolicy;

	private final JCheckBox checRepositoryReleasesEnabled;
	private final UpdatePolicyPanel panelRepositoryReleasesUpdatePolicy;
	private final JComboBox comboRepositoryReleasesChecksumPolicy;

	private final PluginRepositoriesTableModel tablePluginRepositoriesModel;
	private final JTable tablePluginRepositories;

	private final Button bPluginRepositoriesSave;
	private final Button bPluginRepositoriesEdit;
	private final Button bPluginRepositoriesDelete;

	private final JTextField txtPluginRepositoriesId;
	private final JTextField txtPluginRepositoriesName;
	private final JTextField txtPluginRepositoriesUrl;
	private final JComboBox comboPluginRepositoriesLayout;

	private final JCheckBox checPluginRepositorySnapshotEnabled;
	private final UpdatePolicyPanel comboPluginRepositorySnapshotUpdatePolicy;
	private final JComboBox comboPluginRepositorySnapshotChecksumPolicy;

	private final JCheckBox checPluginRepositoryReleasesEnabled;
	private final UpdatePolicyPanel panelPluginRepositoryReleasesUpdatePolicy;
	private final JComboBox comboPluginRepositoryReleasesChecksumPolicy;
	private final Button bRepositoryTestUrl;
	private final Button bPluginRepositoryTestUrl;

	public ProfilesTab(final Settings settings) {
		super(new BorderLayout());
		setBorder(new EmptyBorder(10, 10, 0, 10));
		listProfilesModel = new ListProfilesModel(settings);
		listProfiles = new JList(listProfilesModel);
		listProfiles.setPreferredSize(new Dimension(100, 80));
		listProfiles.setSize(new Dimension(100, 80));
		listProfiles.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				selectInListProfiles(listProfiles.getSelectedIndex());
			}

		});

		listProfiles.addMouseListener(new MouseAdapter() {
			public final void mouseClicked(MouseEvent e) {
				selectInListProfiles(listProfiles.getSelectedIndex());
			}
		});
		JPanel pList = PanelFactory.wrapWithTile("Profiles", new JScrollPane(listProfiles));
		pList.setPreferredSize(new Dimension(100, 80));
		add(pList, BorderLayout.NORTH);

		checkActiveByDefault = new JCheckBox();
		txtActivationJdk = new JTextField();

		JPanel pActivation = PanelFactory.createBorderLayout(0, 0, 0, 0);
		JPanel pActivationEdit = PanelFactory.createGridLayout(2, 1);
		pActivationEdit.add(PanelFactory.wrapWithLabelNorm("Active by Default", checkActiveByDefault, 10));
		pActivationEdit.add(PanelFactory.wrapWithLabelNorm("JDK", txtActivationJdk, 10));

		JPanel pOs = PanelFactory.createBorderLayout("OS");
		JPanel pOsEdit = PanelFactory.createGridLayout(4, 1);
		txtOsName = new JTextField();
		txtOsArch = new JTextField();
		txtOsFamily = new JTextField();
		txtOsVersion = new JTextField();
		pOsEdit.add(PanelFactory.wrapWithLabelNorm("Name", txtOsName, 20));
		pOsEdit.add(PanelFactory.wrapWithLabelNorm("Family", txtOsFamily, 17));
		pOsEdit.add(PanelFactory.wrapWithLabelNorm("Arch", txtOsArch, 26));
		pOsEdit.add(PanelFactory.wrapWithLabelNorm("Version", txtOsVersion, 10));

		pOs.add(pOsEdit, BorderLayout.NORTH);

		txtPropertyName = new JTextField();
		txtPropertyValue = new JTextField();
		JPanel pProperty = PanelFactory.createBorderLayout("Property");
		JPanel pPropertyEdit = PanelFactory.createGridLayout(2, 1);
		pPropertyEdit.add(PanelFactory.wrapWithLabelNorm("Name", txtPropertyName, 10));
		pPropertyEdit.add(PanelFactory.wrapWithLabelNorm("Value", txtPropertyValue, 11));
		pProperty.add(pPropertyEdit, BorderLayout.NORTH);

		txtFileExists = new JTextField();
		txtFileMissing = new JTextField();
		JPanel pFile = PanelFactory.createBorderLayout("File");
		JPanel pFileEdit = PanelFactory.createGridLayout(2, 1);
		pFileEdit.add(PanelFactory.wrapWithLabelNorm("Exists", txtFileExists, 20));
		pFileEdit.add(PanelFactory.wrapWithLabelNorm("Missing", txtFileMissing, 11));
		pFile.add(pFileEdit, BorderLayout.NORTH);

		JPanel pPropFile = PanelFactory.createGridLayout(2, 1);
		pPropFile.add(pProperty);
		pPropFile.add(pFile);

		pActivation.add(pActivationEdit, BorderLayout.NORTH);
		pActivation.add(pOs, BorderLayout.CENTER);
		pActivation.add(PanelFactory.wrapByBorderLayout(pPropFile, BorderLayout.NORTH), BorderLayout.SOUTH);

		JTabbedPane tabbedPane = new JTabbedPane();

		JPanel pProfile = PanelFactory.createBorderLayout(10, 10, 10, 10);
		txtProfileId = new JTextField();
		txtProfileId.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				validateProfileId();
			}
		});

		pProfile.add(PanelFactory.wrapWithLabelNorm("Id:", txtProfileId, 10), BorderLayout.NORTH);
		tabbedPane.addTab("Profile", pProfile);
		tabbedPane.addTab("Activation", pActivation);

		tablePropertiesModel = new PropertiesTableModel();
		tableProperties = new JTable(tablePropertiesModel);
		tableProperties.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableProperties.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				selectInTableProperties(tableProperties.getSelectedRow());
			}

		});
		tableProperties.addMouseListener(new MouseAdapter() {
			public final void mouseClicked(MouseEvent e) {
				selectInTableProperties(tableProperties.getSelectedRow());
			}
		});
		JPanel pProperties = PanelFactory.createBorderLayout(10, 10, 10, 10);
		tabbedPane.addTab("Properties", pProperties);
		pProperties.add(new JScrollPane(tableProperties), BorderLayout.CENTER);

		txtPropertiesName = new JTextField();
		txtPropertiesName.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				checkPropertiesIsEmpty();
			}
		});
		txtPropertiesValue = new JTextField();
		txtPropertiesValue.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				checkPropertiesIsEmpty();
			}
		});
		JPanel pPropertiesEdit = PanelFactory.createGridLayout(2, 1);
		pPropertiesEdit.add(PanelFactory.wrapWithLabelNorm("Name", txtPropertiesName, 10));
		pPropertiesEdit.add(PanelFactory.wrapWithLabelNorm("Value", txtPropertiesValue, 11));

		JPanel pPropertiesTitle = PanelFactory.wrapWithTile("Property", pPropertiesEdit);
		pProperties.add(pPropertiesTitle, BorderLayout.SOUTH);

		bPropertiesSave = new Button("Save", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				tablePropertiesModel.add(txtPropertiesName.getText(), txtPropertiesValue.getText());
				cleanProperties();
			}
		});
		bPropertiesEdit = new Button("Edit", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				tablePropertiesModel.edit(tableProperties.getSelectedRow(), txtPropertiesName.getText(), txtPropertiesValue.getText());
				cleanProperties();
			}
		});
		bPropertiesDelete = new Button("Delete", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				tablePropertiesModel.remove(tableProperties.getSelectedRow());
				cleanProperties();
			}
		});
		pPropertiesTitle.add(PanelFactory.createButtonsPanel(0, 0, 0, -4, bPropertiesSave, bPropertiesEdit, bPropertiesDelete), BorderLayout.SOUTH);

		JPanel pRepositories = PanelFactory.createBorderLayout(10, 10, 10, 10);
		// (0, 0, 0, 0);

		tableRepositoriesModel = new RepositoriesTableModel();
		tableRepositories = new JTable(tableRepositoriesModel);
		tableRepositories.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				selectInTableRepositories(tableRepositories.getSelectedRow());
			}
		});
		tableRepositories.addMouseListener(new MouseAdapter() {
			public final void mouseClicked(MouseEvent e) {
				selectInTableRepositories(tableRepositories.getSelectedRow());
			}
		});
		pRepositories.add(new JScrollPane(tableRepositories), BorderLayout.CENTER);

		txtRepositoriesId = new JTextField();
		txtRepositoriesId.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				checkIfRepositoryIdIsEmpty();
			}
		});

		bRepositoryTestUrl = new Button("...", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				testUrl(txtRepositoriesUrl.getText());
			}
		});
		bRepositoryTestUrl.setPreferredSize(new Dimension(20, 19));

		txtRepositoriesName = new JTextField();
		txtRepositoriesUrl = new JTextField();
		txtRepositoriesUrl.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				bRepositoryTestUrl.setEnabled(validateUrl(txtRepositoriesUrl.getText()));
			}
		});

		comboRepositoriesLayout = new JComboBox(Layout.values());
		comboRepositoriesLayout.setPreferredSize(new Dimension(100, 20));

		JPanel ppRepositoriesButtonUrlTest = PanelFactory.createBorderLayout(0, 0, 0, 1);
		ppRepositoriesButtonUrlTest.add(PanelFactory.wrapWithLabelNorm("Url:", txtRepositoriesUrl, 24), BorderLayout.CENTER);
		ppRepositoriesButtonUrlTest.add(PanelFactory.wrapByBorderLayout(bRepositoryTestUrl, BorderLayout.NORTH), BorderLayout.EAST);

		JPanel pRepositoriesName = PanelFactory.createBorderLayout("Repository");
		JPanel pRepositoriesEdit = PanelFactory.createGridLayout(4, 1);
		pRepositoriesEdit.add(PanelFactory.wrapWithLabelNorm("Id", txtRepositoriesId, 33));
		pRepositoriesEdit.add(PanelFactory.wrapWithLabelNorm("Name", txtRepositoriesName, 10));
		pRepositoriesEdit.add(ppRepositoriesButtonUrlTest);
		pRepositoriesEdit.add(PanelFactory.wrapWithLabelNorm("Layout", comboRepositoriesLayout, 5));
		pRepositoriesName.add(pRepositoriesEdit, BorderLayout.NORTH);

		bRepositoriesSave = new Button("Save", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				tableRepositoriesModel.add(txtRepositoriesId.getText(), txtRepositoriesName.getText(), txtRepositoriesUrl.getText(), comboRepositoriesLayout.getSelectedItem(), checRepositorySnapshotEnabled.isSelected(), comboRepositoryReleasesChecksumPolicy.getSelectedItem(),
						UpdatePolicy.parse(panelRepositoryReleasesUpdatePolicy.getSelectedItem()), checRepositorySnapshotEnabled.isSelected(), comboRepositorySnapshotChecksumPolicy.getSelectedItem(), panelRepositorySnapshotUpdatePolicy.getSelectedItem());
				cleanRepositories();
			}
		});
		bRepositoriesEdit = new Button("Edit", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				tableRepositoriesModel.edit(tableRepositories.getSelectedRow(), txtRepositoriesId.getText(), txtRepositoriesName.getText(), txtRepositoriesUrl.getText(), (Layout) comboRepositoriesLayout.getSelectedItem(), checRepositorySnapshotEnabled.isSelected(),
						comboRepositoryReleasesChecksumPolicy.getSelectedItem(), panelRepositoryReleasesUpdatePolicy.getSelectedItem(), checRepositorySnapshotEnabled.isSelected(), comboRepositorySnapshotChecksumPolicy.getSelectedItem(), panelRepositorySnapshotUpdatePolicy
								.getSelectedItem());
				cleanRepositories();
			}
		});
		bRepositoriesDelete = new Button("Delete", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				tableRepositoriesModel.remove(tableRepositories.getSelectedRow());
				cleanRepositories();
			}
		});
		pRepositoriesName.add(PanelFactory.createButtonsPanel(0, 0, 0, -4, bRepositoriesSave, bRepositoriesEdit, bRepositoriesDelete), BorderLayout.SOUTH);
		JPanel pRepositoryDetails = PanelFactory.createGridLayout(2, 1);

		pRepositoriesName.add(pRepositoryDetails, BorderLayout.CENTER);
		// pRepositoryDetails.add(pRepositoriesName);

		checRepositorySnapshotEnabled = new JCheckBox();
		panelRepositorySnapshotUpdatePolicy = new UpdatePolicyPanel();
		panelRepositorySnapshotUpdatePolicy.setPreferredSize(new Dimension(100, 20));
		comboRepositorySnapshotChecksumPolicy = new JComboBox(ChecksumPolicy.values());
		comboRepositorySnapshotChecksumPolicy.setPreferredSize(new Dimension(100, 20));

		// comboRepositorySnapshotChecksumPolicy.setPreferredSize(new
		// Dimension(txtRepositoriesId.getSize()));
		JPanel pRepositorySnapshot = PanelFactory.createGridLayout(3, 1);
		pRepositorySnapshot.add(PanelFactory.wrapWithLabelNorm("Enabled", checRepositorySnapshotEnabled, 60));
		pRepositorySnapshot.add(PanelFactory.wrapWithLabelNorm("Update Policy", panelRepositorySnapshotUpdatePolicy, 31));
		pRepositorySnapshot.add(PanelFactory.wrapWithLabelNorm("Checksum Policy", comboRepositorySnapshotChecksumPolicy, 10));
		pRepositoryDetails.add(PanelFactory.wrapWithTile("Snapshot", PanelFactory.wrapByBorderLayout(pRepositorySnapshot, BorderLayout.NORTH)));

		checRepositoryReleasesEnabled = new JCheckBox();
		comboRepositoryReleasesChecksumPolicy = new JComboBox(ChecksumPolicy.values());
		comboRepositoryReleasesChecksumPolicy.setPreferredSize(new Dimension(100, 20));
		// comboRepositoryReleasesChecksumPolicy.setPreferredSize(new
		// Dimension(100, 30));
		panelRepositoryReleasesUpdatePolicy = new UpdatePolicyPanel();
		panelRepositoryReleasesUpdatePolicy.setPreferredSize(new Dimension(100, 20));

		JPanel pRepositoryReleases = PanelFactory.createGridLayout(3, 1);
		pRepositoryReleases.add(PanelFactory.wrapWithLabelNorm("Enabled", checRepositoryReleasesEnabled, 60));
		pRepositoryReleases.add(PanelFactory.wrapWithLabelNorm("Update Policy", panelRepositoryReleasesUpdatePolicy, 31));
		pRepositoryReleases.add(PanelFactory.wrapWithLabelNorm("Checksum Policy", comboRepositoryReleasesChecksumPolicy, 10));
		pRepositoryDetails.add(PanelFactory.wrapWithTile("Releases", PanelFactory.wrapByBorderLayout(pRepositoryReleases, BorderLayout.NORTH)));

		// pRepositories.add(pRepositoryDetails, BorderLayout.SOUTH);
		pRepositories.add(pRepositoriesName, BorderLayout.SOUTH);
		tabbedPane.addTab("Repositories", pRepositories);

		JPanel pPluginRepositories = PanelFactory.createBorderLayout(10, 10, 10, 10);

		tablePluginRepositoriesModel = new PluginRepositoriesTableModel();
		tablePluginRepositories = new JTable(tablePluginRepositoriesModel);
		tablePluginRepositories.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				selectInTablePluginRepositories(tablePluginRepositories.getSelectedRow());
			}

		});
		tablePluginRepositories.addMouseListener(new MouseAdapter() {
			public final void mouseClicked(MouseEvent e) {
				selectInTablePluginRepositories(tablePluginRepositories.getSelectedRow());
			}
		});
		pPluginRepositories.add(new JScrollPane(tablePluginRepositories), BorderLayout.CENTER);

		txtPluginRepositoriesId = new JTextField();
		txtPluginRepositoriesId.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				checkIfPluginRepositoryIdIsEmpty();
			}
		});
		txtPluginRepositoriesName = new JTextField();
		txtPluginRepositoriesUrl = new JTextField();
		txtPluginRepositoriesUrl.addKeyListener(new KeyAdapter() {
			public final void keyReleased(KeyEvent e) {
				bPluginRepositoryTestUrl.setEnabled(validateUrl(txtPluginRepositoriesUrl.getText()));
			}
		});

		comboPluginRepositoriesLayout = new JComboBox(Layout.values());
		comboPluginRepositoriesLayout.setPreferredSize(new Dimension(100, 20));

		bPluginRepositoryTestUrl = new Button("...", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				testUrl(txtPluginRepositoriesUrl.getText());
			}
		});
		bPluginRepositoryTestUrl.setPreferredSize(new Dimension(20, 19));

		JPanel pPluginRepositoriesButtonUrlTest = PanelFactory.createBorderLayout(0, 0, 0, 1);
		pPluginRepositoriesButtonUrlTest.add(PanelFactory.wrapWithLabelNorm("Url:", txtPluginRepositoriesUrl, 24), BorderLayout.CENTER);
		pPluginRepositoriesButtonUrlTest.add(PanelFactory.wrapByBorderLayout(bPluginRepositoryTestUrl, BorderLayout.NORTH), BorderLayout.EAST);

		JPanel pPluginRepositoriesName = PanelFactory.createBorderLayout("Plugin Repository");
		JPanel pPluginRepositoriesEdit = PanelFactory.createGridLayout(4, 1);
		pPluginRepositoriesEdit.add(PanelFactory.wrapWithLabelNorm("Id", txtPluginRepositoriesId, 33));
		pPluginRepositoriesEdit.add(PanelFactory.wrapWithLabelNorm("Name", txtPluginRepositoriesName, 10));
		pPluginRepositoriesEdit.add(pPluginRepositoriesButtonUrlTest);
		pPluginRepositoriesEdit.add(PanelFactory.wrapWithLabelNorm("Layout", comboPluginRepositoriesLayout, 5));
		pPluginRepositoriesName.add(pPluginRepositoriesEdit, BorderLayout.NORTH);

		bPluginRepositoriesSave = new Button("Save", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				tablePluginRepositoriesModel.add(txtPluginRepositoriesId.getText(), txtPluginRepositoriesName.getText(), txtPluginRepositoriesUrl.getText(), comboPluginRepositoriesLayout.getSelectedItem(), checPluginRepositorySnapshotEnabled.isSelected(),
						comboPluginRepositoryReleasesChecksumPolicy.getSelectedItem(), panelPluginRepositoryReleasesUpdatePolicy.getSelectedItem(), checPluginRepositorySnapshotEnabled.isSelected(), comboPluginRepositorySnapshotChecksumPolicy.getSelectedItem(),
						comboPluginRepositorySnapshotUpdatePolicy.getSelectedItem());
				cleanPluginRepositories();
			}
		});
		bPluginRepositoriesEdit = new Button("Edit", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				tablePluginRepositoriesModel.edit(tablePluginRepositories.getSelectedRow(), txtPluginRepositoriesId.getText(), txtPluginRepositoriesName.getText(), txtPluginRepositoriesUrl.getText(), (Layout) comboPluginRepositoriesLayout.getSelectedItem(),
						checPluginRepositorySnapshotEnabled.isSelected(), comboPluginRepositoryReleasesChecksumPolicy.getSelectedItem(), panelPluginRepositoryReleasesUpdatePolicy.getSelectedItem(), checPluginRepositorySnapshotEnabled.isSelected(),
						comboPluginRepositorySnapshotChecksumPolicy.getSelectedItem(), comboPluginRepositorySnapshotUpdatePolicy.getSelectedItem());
				cleanPluginRepositories();
			}
		});
		bPluginRepositoriesDelete = new Button("Delete", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				tablePluginRepositoriesModel.remove(tablePluginRepositories.getSelectedRow());
				cleanPluginRepositories();
			}
		});
		pPluginRepositoriesName.add(PanelFactory.createButtonsPanel(0, 0, 0, -4, bPluginRepositoriesSave, bPluginRepositoriesEdit, bPluginRepositoriesDelete), BorderLayout.SOUTH);
		JPanel pPluginRepositoryDetails = PanelFactory.createGridLayout(2, 1);
		// pPluginRepositoryDetails.add(pPluginRepositoriesName);
		pPluginRepositoriesName.add(pPluginRepositoryDetails, BorderLayout.CENTER);

		checPluginRepositorySnapshotEnabled = new JCheckBox();
		comboPluginRepositorySnapshotChecksumPolicy = new JComboBox(ChecksumPolicy.values());
		comboPluginRepositorySnapshotChecksumPolicy.setPreferredSize(new Dimension(100, 20));

		comboPluginRepositorySnapshotUpdatePolicy = new UpdatePolicyPanel();
		comboPluginRepositorySnapshotUpdatePolicy.setPreferredSize(new Dimension(100, 20));

		JPanel pPluginRepositorySnapshot = PanelFactory.createGridLayout(3, 1);
		pPluginRepositorySnapshot.add(PanelFactory.wrapWithLabelNorm("Enabled", checPluginRepositorySnapshotEnabled, 60));
		pPluginRepositorySnapshot.add(PanelFactory.wrapWithLabelNorm("Update Policy", comboPluginRepositorySnapshotUpdatePolicy, 31));
		pPluginRepositorySnapshot.add(PanelFactory.wrapWithLabelNorm("Checksum Policy", comboPluginRepositorySnapshotChecksumPolicy, 10));
		pPluginRepositoryDetails.add(PanelFactory.wrapWithTile("Snapshot", PanelFactory.wrapByBorderLayout(pPluginRepositorySnapshot, BorderLayout.NORTH)));

		checPluginRepositoryReleasesEnabled = new JCheckBox();
		comboPluginRepositoryReleasesChecksumPolicy = new JComboBox(ChecksumPolicy.values());
		comboPluginRepositoryReleasesChecksumPolicy.setPreferredSize(new Dimension(100, 20));

		panelPluginRepositoryReleasesUpdatePolicy = new UpdatePolicyPanel();
		panelPluginRepositoryReleasesUpdatePolicy.setPreferredSize(new Dimension(100, 20));

		JPanel pPluginRepositoryReleases = PanelFactory.createGridLayout(3, 1);
		pPluginRepositoryReleases.add(PanelFactory.wrapWithLabelNorm("Enabled", checPluginRepositoryReleasesEnabled, 60));
		pPluginRepositoryReleases.add(PanelFactory.wrapWithLabelNorm("Update Policy", panelPluginRepositoryReleasesUpdatePolicy, 31));
		pPluginRepositoryReleases.add(PanelFactory.wrapWithLabelNorm("Checksum Policy", comboPluginRepositoryReleasesChecksumPolicy, 10));
		pPluginRepositoryDetails.add(PanelFactory.wrapWithTile("Releases", PanelFactory.wrapByBorderLayout(pPluginRepositoryReleases, BorderLayout.NORTH)));

		pPluginRepositories.add(pPluginRepositoriesName, BorderLayout.SOUTH);
		tabbedPane.addTab("PluginRepositories", pPluginRepositories);

		add(tabbedPane, BorderLayout.CENTER);

		bSave = new Button("Save", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				listProfilesModel.add(txtProfileId.getText().trim(), createAtives(), tablePropertiesModel.getProperties(), tableRepositoriesModel.getRepositories(), tablePluginRepositoriesModel.getRepositories());
				cleanAll();
				listProfiles.updateUI();
			}
		});
		bEdit = new Button("Edit", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				String profileId = txtProfileId.getText().trim();
				String profileIdOld = listProfilesModel.get(listProfiles.getSelectedIndex()).getId();
				boolean activeNewProfile = false;
				if (!profileIdOld.equals(profileId) && settings.isActiveProfile(profileIdOld)) {
					int ans = JOptionPane.showOptionDialog(Desktop.getComponent(), "Profile '" + profileIdOld + "' is active , do you want activate new profile?", "Edited profile is activated", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
					activeNewProfile = (ans == JOptionPane.YES_OPTION);
				}

				listProfilesModel.edit(listProfiles.getSelectedIndex(), activeNewProfile, profileIdOld, profileId, createAtives(), tablePropertiesModel.getProperties(), tableRepositoriesModel.getRepositories(), tablePluginRepositoriesModel.getRepositories());
				cleanAll();
				listProfiles.updateUI();
			}
		});
		bDelete = new Button("Delete", false, new ActionListener() {
			public final void actionPerformed(ActionEvent e) {
				Profile profile = listProfilesModel.get(listProfiles.getSelectedIndex());
				if (settings.isActiveProfile(profile.getId())) {
					int ans = JOptionPane.showOptionDialog(Desktop.getComponent(), "This profile '" + profile.getId() + "' is active. Do you want delete?", "Delete active profile", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
					if (ans == JOptionPane.YES_OPTION) {
						listProfilesModel.remove(profile.getId());
					}
				} else {
					listProfilesModel.remove(profile.getId());
				}
				listProfiles.updateUI();
			}
		});
		add(PanelFactory.createButtonsPanel(0, 0, 0, 0, bSave, bEdit, bDelete), BorderLayout.SOUTH);
	}

	private final Activation createAtives() {
		Activation activation = new Activation();
		// activation.setActiveByDefault(checkActiveByDefault.isSelected());

		if (txtOsArch.getText().trim().length() != 0 || txtOsFamily.getText().trim().length() != 0 || txtOsName.getText().trim().length() != 0 || txtOsVersion.getText().trim().length() != 0) {
			Os os = new Os();
			os.setArch(txtOsArch.getText().trim());
			os.setFamily(txtOsFamily.getText().trim());
			os.setName(txtOsName.getText().trim());
			os.setVersion(txtOsVersion.getText().trim());
			activation.setOs(os);
		}

		if (txtFileExists.getText().trim().length() != 0 || txtFileMissing.getText().trim().length() != 0) {
			File file = new File();
			file.setExists(txtFileExists.getText().trim());
			file.setMissing(txtFileMissing.getText().trim());
			activation.setFile(file);
		}

		if (txtPropertyName.getText().trim().length() != 0 || txtPropertyValue.getText().trim().length() != 0) {
			Property property = new Property();
			property.setName(txtPropertyName.getText().trim());
			property.setValue(txtPropertyValue.getText().trim());
			activation.setProperty(property);
		}

		if (txtActivationJdk.getText().trim().length() == 0) {
			activation.setActiveByDefault(null);
		} else {
			activation.setActiveByDefault(txtActivationJdk.getText().trim());
		}
		return activation;
	}

	private final void cleanAll() {
		txtActivationJdk.setText("");
		txtOsArch.setText("");
		txtOsFamily.setText("");
		txtOsName.setText("");
		txtOsVersion.setText("");
		checkActiveByDefault.setSelected(false);
		txtProfileId.setText("");
		txtFileExists.setText("");
		txtFileMissing.setText("");
		txtPropertyName.setText("");
		txtPropertyValue.setText("");
		cleanProperties();
		cleanRepositories();
		cleanPluginRepositories();

		bSave.setEnabled(false);
		bEdit.setEnabled(false);
		bDelete.setEnabled(false);
	}

	/**
	 * validate profile id
	 */
	private final void validateProfileId() {
		if (txtProfileId.getText().trim().length() == 0) {
			bSave.setEnabled(false);
			bEdit.setEnabled(false);
		} else if (listProfilesModel.exists(txtProfileId.getText())) {
			bSave.setEnabled(false);
		} else {
			bSave.setEnabled(true);
			bEdit.setEnabled(listProfiles.getSelectedIndex() != -1);
		}
	}

	private final boolean validateUrl(String text) {
		if (text.trim().length() == 0) {
			return false;
		} else {
			try {
				new URL(text);
				return true;
			} catch (MalformedURLException e1) {
				return false;
			}
		}
	}

	private final void testUrl(final String url) {
		new Thread(new Runnable() {
			public void run() {
				FrmLoading frmLoading = new FrmLoading("Connection url test...");
				try {
					final URLTester tester = new URLTester(new URL(url));
					frmLoading.showFrm(new HideNotifiListener() {
						public final void hideForm(Object returnObj) {
							tester.cancel();
							return;
						}
					});

					tester.testConnection(URLTester.TIME_OUT_SECONDS);
					frmLoading.hideMe();
				} catch (ConnectException ex) {
					frmLoading.hideMe();
					JOptionPane.showMessageDialog(Desktop.getComponent(), ex.getMessage() + " url:" + url, "Connection error", JOptionPane.ERROR_MESSAGE);
				} catch (MalformedURLException ex) {
					frmLoading.hideMe();
					JOptionPane.showMessageDialog(Desktop.getComponent(), ex.getMessage() + " url:" + url, "Url error", JOptionPane.ERROR_MESSAGE);
				}
			}
		}).start();
	}

	/**
	 * validate id for {@link #txtPluginRepositoriesId}
	 */
	private final void checkIfPluginRepositoryIdIsEmpty() {
		boolean state = txtPluginRepositoriesId.getText().trim().length() != 0 && !tablePluginRepositoriesModel.contains(txtPluginRepositoriesId.getText().trim());
		bPluginRepositoriesSave.setEnabled(state);
		bPluginRepositoriesEdit.setEnabled(tablePluginRepositories.getSelectedRow() != -1);
	}

	/**
	 * validate id for {@link #txtRepositoriesId}
	 */
	private final void checkIfRepositoryIdIsEmpty() {
		boolean state = txtRepositoriesId.getText().trim().length() != 0 && !tableRepositoriesModel.contains(txtRepositoriesId.getText().trim());
		bRepositoriesSave.setEnabled(state);
		bRepositoriesEdit.setEnabled(tableRepositories.getSelectedRow() != -1);
	}

	private final void cleanPluginRepositories() {
		txtPluginRepositoriesId.setText("");
		txtPluginRepositoriesName.setText("");
		comboPluginRepositoriesLayout.setSelectedItem(Layout.EMPTY);
		txtPluginRepositoriesUrl.setText("");

		checPluginRepositoryReleasesEnabled.setSelected(false);
		comboPluginRepositoryReleasesChecksumPolicy.setSelectedItem(ChecksumPolicy.EMPTY);
		panelPluginRepositoryReleasesUpdatePolicy.selectPolicy(UpdatePolicy.EMPTY);

		checPluginRepositorySnapshotEnabled.setSelected(false);
		comboPluginRepositorySnapshotChecksumPolicy.setSelectedItem(ChecksumPolicy.EMPTY);
		comboPluginRepositorySnapshotUpdatePolicy.selectPolicy(UpdatePolicy.EMPTY);

		bPluginRepositoriesDelete.setEnabled(false);
		bPluginRepositoriesEdit.setEnabled(false);
		bPluginRepositoriesSave.setEnabled(false);
	}

	private final void cleanRepositories() {
		txtRepositoriesId.setText("");
		txtRepositoriesName.setText("");
		comboRepositoriesLayout.setSelectedItem(Layout.EMPTY);
		txtRepositoriesUrl.setText("");

		checRepositoryReleasesEnabled.setSelected(false);
		comboRepositoryReleasesChecksumPolicy.setSelectedItem(ChecksumPolicy.EMPTY);
		panelRepositoryReleasesUpdatePolicy.selectPolicy(UpdatePolicy.EMPTY);

		checRepositorySnapshotEnabled.setSelected(false);
		comboRepositorySnapshotChecksumPolicy.setSelectedItem(ChecksumPolicy.EMPTY);
		panelRepositorySnapshotUpdatePolicy.selectPolicy(UpdatePolicy.EMPTY);

		bRepositoriesDelete.setEnabled(false);
		bRepositoriesEdit.setEnabled(false);
		bRepositoriesSave.setEnabled(false);
	}

	private final void checkPropertiesIsEmpty() {
		if (txtPropertiesName.getText().trim().length() == 0) {
			bPropertiesEdit.setEnabled(false);
			bPropertiesSave.setEnabled(false);
		} else if (txtPropertiesValue.getText().trim().length() == 0) {
			bPropertiesEdit.setEnabled(false);
			bPropertiesSave.setEnabled(false);
		} else if (tablePropertiesModel.contains(txtPropertiesName.getText())) {
			bPropertiesSave.setEnabled(false);
			bPropertiesEdit.setEnabled(tableProperties.getSelectedRow() != -1);
		} else {
			bPropertiesSave.setEnabled(true);
			bPropertiesEdit.setEnabled(tableProperties.getSelectedRow() != -1);
		}
	}

	private final void cleanProperties() {
		bPropertiesDelete.setEnabled(false);
		bPropertiesEdit.setEnabled(false);
		bPropertiesSave.setEnabled(false);
		txtPropertiesName.setText("");
		txtPropertiesValue.setText("");
	}

	/**
	 * select in {@link #tableProperties}
	 * 
	 * @param selectedRow
	 */
	private final void selectInTableProperties(int selectedRow) {
		if (selectedRow == -1) {
			bPropertiesDelete.setEnabled(false);
			bPropertiesEdit.setEnabled(false);
		} else {
			Object key = tablePropertiesModel.get(selectedRow);
			txtPropertiesName.setText(key.toString());
			txtPropertiesValue.setText(tablePropertiesModel.getValue(key).toString());
			bPropertiesDelete.setEnabled(true);
			bPropertiesEdit.setEnabled(true);
		}
	}

	/**
	 * select in {@link #tablePluginRepositories}
	 * 
	 * @param selectedRow
	 */
	private final void selectInTablePluginRepositories(int selectedRow) {
		PluginRepository repository = tablePluginRepositoriesModel.get(selectedRow);
		txtPluginRepositoriesId.setText(repository.getId());
		txtPluginRepositoriesName.setText(repository.getName());
		txtPluginRepositoriesUrl.setText(repository.getUrl());
		comboPluginRepositoriesLayout.setSelectedItem(repository.getLayout());

		if (repository.getReleases() != null) {
			Release release = repository.getReleases();
			comboRepositoryReleasesChecksumPolicy.setSelectedItem(release.getChecksumPolicy());
			panelPluginRepositoryReleasesUpdatePolicy.selectPolicy(release.getUpdatePolicy());
			panelPluginRepositoryReleasesUpdatePolicy.selectInteval(release.getUpdatePolicyInterval());

			checPluginRepositoryReleasesEnabled.setSelected(release.isEnabled());
		}
		if (repository.getSnapshots() != null) {
			Snapshot snapshot = repository.getSnapshots();
			comboRepositorySnapshotChecksumPolicy.setSelectedItem(snapshot.getChecksumPolicy());
			comboPluginRepositorySnapshotUpdatePolicy.selectPolicy(snapshot.getUpdatePolicy());
			comboPluginRepositorySnapshotUpdatePolicy.selectInteval(snapshot.getUpdatePolicyInterval());
			checPluginRepositorySnapshotEnabled.setSelected(snapshot.isEnabled());
		}
		bPluginRepositoriesSave.setEnabled(false);
		bPluginRepositoriesEdit.setEnabled(true);
		bPluginRepositoriesDelete.setEnabled(true);
		bPluginRepositoryTestUrl.setEnabled(validateUrl(txtPluginRepositoriesUrl.getText()));
	}

	/**
	 * select in {@link #tableRepositories}
	 * 
	 * @param selectedRow
	 */
	private final void selectInTableRepositories(int selectedRow) {
		Repository repository = tableRepositoriesModel.get(selectedRow);
		txtRepositoriesId.setText(repository.getId());
		txtRepositoriesName.setText(repository.getName());
		txtRepositoriesUrl.setText(repository.getUrl());
		comboRepositoriesLayout.setSelectedItem(repository.getLayout());

		if (repository.getReleases() != null) {
			Release release = repository.getReleases();
			comboRepositoryReleasesChecksumPolicy.setSelectedItem(release.getChecksumPolicy());
			panelRepositoryReleasesUpdatePolicy.selectPolicy(release.getUpdatePolicy());
			panelRepositoryReleasesUpdatePolicy.selectInteval(release.getUpdatePolicyInterval());
			checRepositoryReleasesEnabled.setSelected(release.isEnabled());
		}
		if (repository.getSnapshots() != null) {
			Snapshot snapshot = repository.getSnapshots();
			comboRepositorySnapshotChecksumPolicy.setSelectedItem(snapshot.getChecksumPolicy());
			panelRepositorySnapshotUpdatePolicy.selectPolicy(snapshot.getUpdatePolicy());
			panelRepositorySnapshotUpdatePolicy.selectInteval(snapshot.getUpdatePolicyInterval());
			checRepositorySnapshotEnabled.setSelected(snapshot.isEnabled());
		}
		bRepositoriesSave.setEnabled(false);
		bRepositoriesEdit.setEnabled(true);
		bRepositoriesDelete.setEnabled(true);
		bRepositoryTestUrl.setEnabled(validateUrl(txtRepositoriesUrl.getText()));
	}

	/**
	 * select line in list of {@link Profile}
	 * 
	 * @param index
	 */
	private final void selectInListProfiles(int index) {
		Profile profile = listProfilesModel.get(index);
		txtProfileId.setText(profile.getId());
		if (profile.getActivation() != null) {
			Activation activation = profile.getActivation();
			txtActivationJdk.setText(activation.getJdk());
			checkActiveByDefault.setSelected(activation.isActiveByDefault());
			if (activation.getOs() != null) {
				Os os = activation.getOs();
				txtOsArch.setText(os.getArch());
				txtOsFamily.setText(os.getFamily());
				txtOsName.setText(os.getName());
				txtOsVersion.setText(os.getVersion());
			} else {
				txtOsArch.setText("");
				txtOsFamily.setText("");
				txtOsName.setText("");
				txtOsVersion.setText("");
			}

			if (activation.getProperty() != null) {
				Property property = activation.getProperty();
				txtPropertyName.setText(property.getName());
				txtPropertyValue.setText(property.getValue());
			} else {
				txtPropertyName.setText("");
				txtPropertyValue.setText("");
			}
			if (activation.getFile() != null) {
				File file = activation.getFile();
				txtFileExists.setText(file.getExists());
				txtFileMissing.setText(file.getMissing());
			} else {
				txtFileExists.setText("");
				txtFileMissing.setText("");
			}
		}
		if (!profile.getProperties().isEmpty()) {
			tablePropertiesModel.set(profile.getProperties());
		}

		tableRepositoriesModel.set(profile.getRepositories());
		tablePluginRepositoriesModel.set(profile.getPluginRepositories());
		cleanProperties();
		cleanRepositories();
		cleanPluginRepositories();

		bEdit.setEnabled(true);
		bDelete.setEnabled(true);

	}

	/**
	 * list of {@link Profile}
	 * 
	 * @author Frastia Tomas
	 * 
	 */
	private final class ListProfilesModel extends AbstractListModel {

		private static final long serialVersionUID = -2673219807127819955L;
		private final Settings settings;

		public ListProfilesModel(Settings settings) {
			this.settings = settings;
		}

		private final void add(String id, Activation activation, Properties properties, List<Repository> repositories, List<PluginRepository> pluginRepositories) {
			Profile profile = new Profile(id);
			profile.setActivation(activation);
			profile.setProperty(properties);
			profile.setRepositories(repositories);
			profile.setPluginRepositories(pluginRepositories);
			settings.addProfile(profile);
		}

		/**
		 * edit
		 * 
		 * @param index
		 * @param activeNewProfile
		 * @param idOld
		 * @param id
		 * @param activation
		 * @param properties
		 * @param repositories
		 * @param pluginRepositories
		 */
		private final void edit(int index, boolean activeNewProfile, String idOld, String id, Activation activation, Properties properties, List<Repository> repositories, List<PluginRepository> pluginRepositories) {
			Profile profile = settings.getProfiles().get(index);
			profile.setId(id);
			profile.setProperty(properties);
			profile.setActivation(activation);
			profile.setRepositories(repositories);
			profile.setPluginRepositories(pluginRepositories);
			settings.getProfiles().set(index, profile);
			settings.removeActiveProfile(idOld);
			if (activeNewProfile) {
				settings.addActiveProfile(id);
			}
		}

		private boolean exists(String id) {
			return settings.existsProfile(id);
		}

		private void remove(String id) {
			settings.removeProfile(id);
		}

		private final Profile get(int index) {
			return settings.getProfiles().get(index);
		}

		public final Object getElementAt(int index) {
			return settings.getProfiles().get(index);
		}

		public final int getSize() {
			return settings.getProfiles().size();
		}

	}

	/**
	 * {@link Properties}
	 * 
	 * @author Frastia Tomas
	 * 
	 */
	private final class PropertiesTableModel extends AbstractTableModel {

		private static final long serialVersionUID = -6933472168266292927L;

		private final String[] columns = new String[] { "Name", "Value" };

		private List<Object> keys;
		private final Properties properties;

		private PropertiesTableModel() {
			keys = new ArrayList<Object>();
			properties = new Properties();
		}

		private final Properties getProperties() {
			return properties;
		}

		private final boolean contains(String key) {
			return keys.contains(key.trim());
		}

		private final void remove(int selectedRow) {
			properties.remove(keys.remove(selectedRow));
			fireTableDataChanged();
		}

		private final void edit(int index, String key, String value) {
			properties.remove(keys.get(index));
			keys.set(index, key);
			properties.put(key, value);
			fireTableDataChanged();
		}

		private final void add(String key, String value) {
			keys.add(key);
			properties.put(key, value);
			fireTableDataChanged();
		}

		private final Object get(int selectedRow) {
			return keys.get(selectedRow);
		}

		private final Object getValue(Object key) {
			return properties.get(key);
		}

		private final void set(Properties properties) {
			this.properties.clear();
			this.keys = Arrays.asList(properties.keySet().toArray());
			this.properties.putAll(properties);
			fireTableDataChanged();
		}

		public final String getColumnName(int column) {
			return columns[column];
		}

		public final int getColumnCount() {
			return columns.length;
		}

		public final int getRowCount() {
			return properties.size();
		}

		public final Object getValueAt(int rowIndex, int columnIndex) {
			if (columnIndex == 1) {
				return properties.get(keys.get(rowIndex));
			}
			return keys.get(rowIndex);
		}
	}

	/**
	 * 
	 * {@link Repository}
	 * 
	 * @author Frastia Tomas
	 * 
	 */
	private final class RepositoriesTableModel extends AbstractTableModel {

		private static final long serialVersionUID = -6933472168266292927L;

		private final String[] columns = new String[] { "Id", "Name" };

		private final List<Repository> repositories;

		private RepositoriesTableModel() {
			repositories = new ArrayList<Repository>();
		}

		private final List<Repository> getRepositories() {
			return repositories;
		}

		/**
		 * remove {@link Repository}
		 * 
		 * @param selectedRow
		 */
		private final void remove(int selectedRow) {
			repositories.remove(selectedRow);
			fireTableDataChanged();
		}

		/**
		 * edit {@link Repository}
		 * 
		 * @param selectedRow
		 * @param id
		 * @param name
		 * @param url
		 * @param layout
		 * @param releasesEnabled
		 * @param releasesChecksumPolicy
		 * @param releasesUpdatePolicy
		 * @param shapshotEnabled
		 * @param snapshotChecksumPolicy
		 * @param snapshotUpdatePolicty
		 */
		public final void edit(int selectedRow, String id, String name, String url, Layout layout, boolean releasesEnabled, Object releasesChecksumPolicy, Object releasesUpdatePolicy, boolean shapshotEnabled, Object snapshotChecksumPolicy, String snapshotUpdatePolicty) {
			Repository repository = repositories.get(selectedRow);
			repository.setId(id);
			repository.setName(name);
			repository.setUrl(url);
			repository.setLayout(layout);

			if (repository.getReleases() == null) {
				repository.setReleases(new Release());
			}
			repository.getReleases().setEnabled(String.valueOf(releasesEnabled));
			repository.getReleases().setChecksumPolicy(releasesChecksumPolicy.toString());
			repository.getReleases().setUpdatePolicy(releasesUpdatePolicy.toString());

			if (repository.getSnapshots() == null) {
				repository.setSnapshots(new Snapshot());
			}
			repository.getSnapshots().setEnabled(String.valueOf(shapshotEnabled));
			repository.getSnapshots().setChecksumPolicy(snapshotChecksumPolicy.toString());
			repository.getSnapshots().setUpdatePolicy(snapshotUpdatePolicty);
			fireTableDataChanged();
		}

		/**
		 * add new {@link Repository}
		 * 
		 * @param id
		 * @param name
		 * @param url
		 * @param layout
		 * @param releasesEnabled
		 * @param releasesChecksumPolicy
		 * @param releasesUpdatePolicy
		 * @param shapshotEnabled
		 * @param snapshotChecksumPolicy
		 * @param snapshotUpdatePolicty
		 */
		public final void add(String id, String name, String url, Object layout, boolean releasesEnabled, Object releasesChecksumPolicy, UpdatePolicy releasesUpdatePolicy, boolean shapshotEnabled, Object snapshotChecksumPolicy, String snapshotUpdatePolicty) {
			Repository repository = new Repository();
			repository.setId(id);
			repository.setName(name);
			repository.setUrl(url);
			repository.setLayout(layout.toString());

			if (repository.getReleases() == null) {
				repository.setReleases(new Release());
			}
			repository.getReleases().setEnabled(String.valueOf(releasesEnabled));
			repository.getReleases().setChecksumPolicy(releasesChecksumPolicy.toString());
			repository.getReleases().setUpdatePolicy(releasesUpdatePolicy.toString());

			if (repository.getSnapshots() == null) {
				repository.setSnapshots(new Snapshot());
			}
			repository.getSnapshots().setEnabled(String.valueOf(shapshotEnabled));
			repository.getSnapshots().setChecksumPolicy(snapshotChecksumPolicy.toString());
			repository.getSnapshots().setUpdatePolicy(snapshotUpdatePolicty);
			repositories.add(repository);
			fireTableDataChanged();
		}

		private final boolean contains(String id) {
			for (Repository repository : repositories) {
				if (repository.getId().equals(id)) {
					return true;
				}
			}
			return false;
		}

		private final Repository get(int selectedRow) {
			return this.repositories.get(selectedRow);
		}

		private final void set(List<Repository> repositories) {
			this.repositories.clear();
			this.repositories.addAll(repositories);
			fireTableDataChanged();
		}

		public final String getColumnName(int column) {
			return columns[column];
		}

		public final int getColumnCount() {
			return columns.length;
		}

		public final int getRowCount() {
			return repositories.size();
		}

		public final Object getValueAt(int rowIndex, int columnIndex) {
			if (columnIndex == 1) {
				return repositories.get(rowIndex).getName();
			}
			return repositories.get(rowIndex).getId();
		}
	}

	/**
	 * {@link PluginRepository}
	 * 
	 * @author Frastia Tomas
	 * 
	 */
	private final class PluginRepositoriesTableModel extends AbstractTableModel {

		private static final long serialVersionUID = -6933472168266292927L;

		private final String[] columns = new String[] { "Id", "Name" };

		private final List<PluginRepository> pluginRepositories;

		private PluginRepositoriesTableModel() {
			pluginRepositories = new ArrayList<PluginRepository>();
		}

		private final List<PluginRepository> getRepositories() {
			return pluginRepositories;
		}

		private final boolean contains(String id) {
			for (PluginRepository repository : pluginRepositories) {
				if (repository.getId().equals(id)) {
					return true;
				}
			}
			return false;
		}

		/**
		 * add new {@link PluginRepository}
		 * 
		 * @param id
		 * @param name
		 * @param url
		 * @param layout
		 * @param releasesEnabled
		 * @param releasesChecksumPolicy
		 * @param releasesUpdatePolicy
		 * @param shapshotEnabled
		 * @param snapshotChecksumPolicy
		 * @param snapshotUpdatePolicty
		 */
		private final void add(String id, String name, String url, Object layout, boolean releasesEnabled, Object releasesChecksumPolicy, Object releasesUpdatePolicy, boolean shapshotEnabled, Object snapshotChecksumPolicy, Object snapshotUpdatePolicty) {
			PluginRepository repository = new PluginRepository();
			repository.setId(id);
			repository.setName(name);
			repository.setUrl(url);
			repository.setLayout(layout.toString());

			if (repository.getReleases() == null) {
				repository.setReleases(new Release());
			}
			repository.getReleases().setEnabled(String.valueOf(releasesEnabled));
			repository.getReleases().setChecksumPolicy(releasesChecksumPolicy.toString());
			repository.getReleases().setUpdatePolicy(releasesUpdatePolicy.toString());

			if (repository.getSnapshots() == null) {
				repository.setSnapshots(new Snapshot());
			}
			repository.getSnapshots().setEnabled(String.valueOf(shapshotEnabled));
			repository.getSnapshots().setChecksumPolicy(snapshotChecksumPolicy.toString());
			repository.getSnapshots().setUpdatePolicy(snapshotUpdatePolicty.toString());
			pluginRepositories.add(repository);
			fireTableDataChanged();
		}

		/**
		 * edit {@link PluginRepository}
		 * 
		 * @param selectedRow
		 * @param id
		 * @param name
		 * @param url
		 * @param layout
		 * @param releasesEnabled
		 * @param releasesChecksumPolicy
		 * @param releasesUpdatePolicy
		 * @param shapshotEnabled
		 * @param snapshotChecksumPolicy
		 * @param snapshotUpdatePolicty
		 */
		public final void edit(int selectedRow, String id, String name, String url, Layout layout, boolean releasesEnabled, Object releasesChecksumPolicy, Object releasesUpdatePolicy, boolean shapshotEnabled, Object snapshotChecksumPolicy, Object snapshotUpdatePolicty) {
			PluginRepository repository = pluginRepositories.get(selectedRow);
			repository.setId(id);
			repository.setName(name);
			repository.setUrl(url);
			repository.setLayout(layout);

			if (repository.getReleases() == null) {
				repository.setReleases(new Release());
			}
			repository.getReleases().setEnabled(String.valueOf(releasesEnabled));
			repository.getReleases().setChecksumPolicy(releasesChecksumPolicy.toString());
			repository.getReleases().setUpdatePolicy(releasesUpdatePolicy.toString());

			if (repository.getSnapshots() == null) {
				repository.setSnapshots(new Snapshot());
			}
			repository.getSnapshots().setEnabled(String.valueOf(shapshotEnabled));
			repository.getSnapshots().setChecksumPolicy(snapshotChecksumPolicy.toString());
			repository.getSnapshots().setUpdatePolicy(snapshotUpdatePolicty.toString());
			fireTableDataChanged();
		}

		/**
		 * remove {@link PluginRepository}
		 * 
		 * @param selectedRow
		 */
		private final void remove(int selectedRow) {
			pluginRepositories.remove(selectedRow);
			fireTableDataChanged();
		}

		private final PluginRepository get(int selectedRow) {
			return pluginRepositories.get(selectedRow);
		}

		private final void set(List<PluginRepository> pluginRepositories) {
			this.pluginRepositories.clear();
			this.pluginRepositories.addAll(pluginRepositories);
			fireTableDataChanged();
		}

		public final String getColumnName(int column) {
			return columns[column];
		}

		public final int getColumnCount() {
			return columns.length;
		}

		public final int getRowCount() {
			return pluginRepositories.size();
		}

		public final Object getValueAt(int rowIndex, int columnIndex) {
			if (columnIndex == 1) {
				return pluginRepositories.get(rowIndex).getName();
			}
			return pluginRepositories.get(rowIndex).getId();
		}
	}
}
